﻿// test1.cpp: 定义应用程序的入口点。
//
#include "wrapper/checkerror.h"
#include "glframework/core.h"
#include "glframework/shader.h"
#include <iostream>
#include "application/Application.h"


using namespace std;

GLuint vao;
Shader* shader = nullptr;

/*
* 创建窗体的过程
* 1、初始化GLFW的基本环境
* 2、创建窗体对象
* 3、执行窗体循环
*/

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

//设置窗口的宽高
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

//void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mods)
//{
//    if (key == GLFW_KEY_ESCAPE)
//        glfwSetWindowShouldClose(window, true);
//}

void OnResize(int width, int height) {
    std::cout << "OnResize" << std::endl;
    glViewport(0, 0, width, height);
}

void OnkeyCallBack(int key, int action, int mods)
{
    std::cout << key << std::endl;
        
}

void prepareVao()
{
    //1、数据
    float positions[] = {
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
        0.0f, 0.5f, 0.0f,
    };

    float colors[] = {
        1.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 1.0f,
    };

    unsigned int indices[] = {
        0, 1, 2,
    };
    //2、VBO创建
    GLuint posVbo, colorVbo;
    glGenBuffers(1, &posVbo);
    glBindBuffer(GL_ARRAY_BUFFER, posVbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

    glGenBuffers(1, &colorVbo);
    glBindBuffer(GL_ARRAY_BUFFER, colorVbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);


    //3.EBO创建
    GLuint ebo;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    //4 VAO创建
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    //5 vbo ebo加入属性描述信息
    glBindBuffer(GL_ARRAY_BUFFER, posVbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

    glBindBuffer(GL_ARRAY_BUFFER, colorVbo);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);

    glBindVertexArray(0);

}

void prepareShader()
{
    shader = new Shader("./assets/shader_test5/vertex.glsl", "./assets/shader_test5/fragment.glsl");
}

void render() {
    //
    GL_CALL(glClear(GL_COLOR_BUFFER_BIT));

    shader->begin();

    //shader->setFloat("time", glfwGetTime());
    //shader->setFloat("speed", 2.0);

    //shader->setVector3("uColor", 0.3, 0.4, 0.5);
    float color[] = { 0.9, 0.3, 0.25 };

    shader->setVector3("uColor", color);

    glBindVertexArray(vao);
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);

    shader->end();
}

int main()
{
    if(!app->init(800, 600)){
        return -1;
    }
    std::cout << "app init" << std::endl;

    // //窗体size回调
    // glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // //设置按键监听，监听回调
    // glfwSetKeyCallback(window, keyCallBack);
    app->setResizeCallback(OnResize);
    app->setKeyBoardCallback(OnkeyCallBack);

    glViewport(0, 0, 800, 600);
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

    prepareShader();
    prepareVao();
    //窗体循环
    //渲染
    while (app->update())
    {

        // render
       // ------
        //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        //glClear(GL_COLOR_BUFFER_BIT);

        render();

    }

    //释放
    app->destory();
    return 0;
}



//void framebuffer_size_callback(GLFWwindow* window, int width, int height)
//{
//    glViewport(0, 0, width, height);
//}